package com.woniu.p2p.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.UserZong;
import com.woniu.p2p.service.MymoneyService;
import com.woniu.p2p.vo.FindMyMoneyHistoryVo;
import com.woniu.p2p.vo.MymoneyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/mymoney")
public class P2PMyMoney {
    @Autowired
    MymoneyService mymoneyService;

    @RequestMapping("/mymoneyOfzong")
    public MymoneyVo findAllMyMoney(HttpServletRequest request){
        String token = request.getHeader("token");
        int i = Integer.parseInt(token);
        MymoneyVo allMyMoney = mymoneyService.findAllMyMoney(i);
        return allMyMoney;
    }

    @RequestMapping("/mymoneyHistory")
    public PageInfo<UserZong> findAllMyMoneyHistory(@RequestBody FindMyMoneyHistoryVo findMyMoneyHistoryVo, HttpServletRequest request){
        String token = request.getHeader("token");
        int i = Integer.parseInt(token);
        if(findMyMoneyHistoryVo.getMymoneytime()!=null){
            Integer currentPage = findMyMoneyHistoryVo.getCurrentPage();
            Integer pagesize = findMyMoneyHistoryVo.getPagesize();
            String mymoneyKeyword = findMyMoneyHistoryVo.getMymoneyKeyword();
            String[] mymoneytime = findMyMoneyHistoryVo.getMymoneytime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse1 = null;
            Date parse2= null;
            try {
                parse1 = sdf.parse(mymoneytime[0]);
                parse2 = sdf.parse(mymoneytime[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            PageInfo<UserZong> pageInfo = mymoneyService.findAllMyMoneyByKeywordAndTime(i, currentPage, pagesize, mymoneyKeyword, parse1, parse2);
            return pageInfo;
        }else {
            Integer currentPage = findMyMoneyHistoryVo.getCurrentPage();
            Integer pagesize = findMyMoneyHistoryVo.getPagesize();
            String mymoneyKeyword = findMyMoneyHistoryVo.getMymoneyKeyword();
            Date parse1 = null;
            Date parse2= null;
            PageInfo<UserZong> pageInfo = mymoneyService.findAllMyMoneyByKeywordAndTime(i, currentPage, pagesize, mymoneyKeyword, parse1, parse2);
            return pageInfo;
        }
    }

    @RequestMapping("/mymoneyHistory2")
    public PageInfo<UserZong> findAllMyMoneyHistory2(HttpServletRequest request,@RequestBody FindMyMoneyHistoryVo findMyMoneyHistoryVo){
        String token = request.getHeader("token");
        int i = Integer.parseInt(token);
        Integer currentPage = findMyMoneyHistoryVo.getCurrentPage();
        Integer pagesize = findMyMoneyHistoryVo.getPagesize();
        PageInfo<UserZong> pageInfo = mymoneyService.findAllmymoneyHistory(i, currentPage, pagesize);
        return pageInfo;
    }

    @RequestMapping("/findmoneybyid")
    public UserZong findmoneybyid(@RequestBody UserZong userZong, HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        Integer zongId = userZong.getZongId();
        UserZong userZong1=mymoneyService.selectbyid(zongId);
        return userZong1;
    }
}
